package hu.pasq.dipterv.business;

import hu.pasq.dipterv.business.jms.MessageSender;
import hu.pasq.dipterv.domain.Hint;
import hu.pasq.dipterv.service.HintService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/hint")
public class HintBO {

	@Autowired
	HintService hintService;

	@Autowired
	MessageSender messageSender;

	private static final Logger logger = LoggerFactory.getLogger(HintBO.class);

	@RequestMapping("/")
	public String listAll( Model uiModel) {
		logger.info("findAll ");
		List<Hint> hints = hintService.findAll();
		uiModel.addAttribute("hints", hints);
		return "hints";
	}
	
	@RequestMapping("/boot")
	public String boot( Model uiModel) {
		logger.info("findAll ");

		uiModel.addAttribute("asd", "asdASD");
		return "boot";
	}
	
	
	
	@RequestMapping("/new")
	public String createHint(Model uiModel) {
		logger.info("new");
		Hint hint = new Hint();
		hint.setName("ASD" + System.currentTimeMillis());
		hintService.save(hint);
		uiModel.addAttribute("hint", hint);
		return "hint";
	}

	@RequestMapping("/find/{name}")
	public String findByName(@PathVariable("name") String name, Model uiModel) {
		logger.info("find " + name);
		Hint hint = hintService.findByName(name);
		Hint hint2 = hintService.find(2L);
		Hint hint3 = hintService.findOne(2L);
		
		
		uiModel.addAttribute("hint", hint);
		return "hints";
	}

	@RequestMapping("/send")
	public String send() {
		Map map = new HashMap();
		map.put("Name", "MYNAME");
		Hint hint = new Hint();
		hint.setName("ASD" + System.currentTimeMillis());

		// send to default destination
		messageSender.send("hello JMS");

		return "hint";
	}

	@RequestMapping(value = "/save", params = { "save" })
	public String save(final Hint hint, final BindingResult bindingResult, final ModelMap model) {
		if (bindingResult.hasErrors()) {
			return "hints";
		}
		this.hintService.save(hint);
		model.clear();
		return "redirect:/save";
	}
}
